package com.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manage.mapper.OrdersListMapper;
import com.manage.mapper.OrdersMapper;
import com.manage.pojo.Orders;
import com.manage.pojo.operation.CollectionsList;
import com.manage.pojo.operation.OrdersList;
import com.manage.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrdersListMapper ordersListMapper;
    @Autowired
    private Sid oid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryOrdersIsExit(String orderId) {
        Orders order = new Orders();
        order.setOrderId(orderId);
        Orders result = ordersMapper.selectOne(order);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveOrders(Orders order) {
        String orderId = oid.nextShort();
        order.setOrderId(orderId);
        ordersMapper.insert(order);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void orderDelete(String orderId) {
        Example orderExample = new Example(Orders.class);
        Criteria criteria = orderExample.createCriteria();
        criteria.andEqualTo("orderId", orderId);
        ordersMapper.deleteByExample(orderExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void orderUpdate(Orders order) {
        Example orderExample = new Example(Orders.class);
        Criteria criteria = orderExample.createCriteria();
        criteria.andEqualTo("orderId", order.getOrderId());
        ordersMapper.updateByExample(order, orderExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public OrdersList queryOrdersInfoByOrderId(String orderId) {
        OrdersList result = ordersListMapper.queryOrdersByOrderId(orderId);
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryOrdersInfoByUserId(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page, pageSize);
        List<OrdersList> list= ordersListMapper.queryOrdersByUserId(userId);
        PageInfo<OrdersList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryOrdersInfoByShoesId(Integer page, Integer pageSize, String shoesId) {
        PageHelper.startPage(page, pageSize);
        List<OrdersList> list= ordersListMapper.queryOrdersByShoesId(shoesId);
        PageInfo<OrdersList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedResult queryAllOrders(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<OrdersList> list= ordersListMapper.queryAllOrders();
        PageInfo<OrdersList> pageList = new PageInfo<>(list);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(list);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }
}
